package cn.wolfcode.luowowo.comment.service.impl;

import cn.wolfcode.luowowo.comment.domain.UserInfoItem;
import cn.wolfcode.luowowo.comment.repository.UserInfoItemRepository;
import cn.wolfcode.luowowo.comment.service.IUserInfoItemService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

@Service
public class UserInfoItemServiceImpl implements IUserInfoItemService {
    @Autowired
    private UserInfoItemRepository repository;
    @Autowired
    private MongoTemplate template;


    public void saveOrUpdate(UserInfoItem userInfoItem) {
        repository.save(userInfoItem);
    }

    public UserInfoItem   get(Long uid) {
        Query query = new Query();
        query.addCriteria(Criteria.where("uid").is(uid));
        UserInfoItem userInfoItem = template.findOne(query, UserInfoItem.class);
        if(userInfoItem == null){
            UserInfoItem infoItem = new UserInfoItem();
            infoItem.setUid(uid);
            infoItem.setScore(10000L);
            this.saveOrUpdate(infoItem);
        }
        return userInfoItem;
    }
}
